﻿using Sliver.Roadnet.RawInfo;
using Sliver.Utility;
using System.Collections.Generic;
using UnityEngine;

namespace Sliver.Roadnet.Components
{
    public class RoadEntity : MonoBehaviour
    {
        public RawRoadInfo RoadInfo { get; private set; }
        public int Index { get; private set; }

        public void Setup(RawRoadInfo info, int index, Transform parent)
        {
            RoadInfo = info;
            Index = index;

            gameObject.name = info.id;
            transform.SetParent(parent);

            IntersectionJsonData start = RoadnetEntity.Instance.GetIntersectionUnsafe(info.startIntersection).JsonData;
            IntersectionJsonData end = RoadnetEntity.Instance.GetIntersectionUnsafe(info.endIntersection).JsonData;

            // start.roadLinks.
            DrawPath(info.points);
        }


        public List<Vector3> Points; //路径点
        private List<MeshRenderer> lines = new List<MeshRenderer>(); //显示的路径

        public float xscale = 1f; //缩放比例
        public float yscale = 1f;

        // protected void Awake()
        // {
        //     meshRenderer = GetComponent<MeshRenderer>();
        //     //箭头宽度缩放值
        //     xscale = meshRenderer.transform.localScale.x;
        //     //箭头长度缩放值
        //     yscale = meshRenderer.transform.localScale.y;
        // }

        protected Transform Parent;

        public void Setup(Transform parent)
        {
            Parent = parent;
            transform.SetParent(parent);
        }

        //画路径
        public void DrawPath(List<Vector2> points)
        {
            Points = new List<Vector3>();
            foreach (Vector2 point in points)
            {
                Points.Add(point.xz3());
                // Debug.Log(point.xz3());
                // Points.Add(new Vector3(point.x, point.y));
            }

            if (Points.Count <= 1) return;
            for (int i = 0; i < Points.Count - 1; i++)
            {
                DrawLine(Points[i], Points[i + 1], i);
            }
        }

        public void DrawPath(List<Vector3> points)
        {
            Points = points;
            if (points == null || points.Count <= 1)
                return;
            for (int i = 0; i < points.Count - 1; i++)
            {
                DrawLine(points[i], points[i + 1], i);
            }
        }

        //画路径 参数为路径点数组
        // public void DrawPath(Vector3[] points)
        // {
        //     if (points == null || points.Length <= 1)
        //         return;
        //     for (int i = 0; i < points.Length - 1; i++)
        //     {
        //         DrawLine(points[i], points[i + 1], i);
        //     }
        // }

        //隐藏路径
        public void HidePath()
        {
            for (int i = 0; i < lines.Count; i++)
                lines[i].gameObject.SetActive(false);
        }

        //画路径
        private void DrawLine(Vector3 start, Vector3 end, int index)
        {
            // Debug.Log(transform.gameObject.name);
            MeshRenderer mr;
            if (index >= lines.Count)
            {
                mr = Instantiate(RoadnetEntity.NavigateArrow, Parent);
                lines.Add(mr);
            }
            else
            {
                mr = lines[index];
            }

            var tran = mr.transform;
            var length = Vector3.Distance(start, end);
            tran.localScale = new Vector3(xscale, 1, length);
            tran.position = (start + end) / 2;
            // Debug.Log(tran.position);
            //指向end
            tran.LookAt(end);
            //旋转偏移
            // tran.Rotate(90, 0, 0);
            mr.material.mainTextureScale = new Vector2(1, length * yscale);
            mr.gameObject.SetActive(true);
        }
    }
}